

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

#include "common/utils.hpp"

// 层序遍历后，计算每层平均值
std::vector<double> averageOfLevels(TreeNode* root) {
    std::queue<TreeNode*> queue;

    if (root != nullptr) queue.push(root);

    std::vector<double> result;
    while (!queue.empty())
    {
        int size = queue.size();
        double sum = 0;
        for (int i = 0; i < size; i++) {
            TreeNode* node = queue.front();
            queue.pop();
            sum += node->val;

            if (node->left) queue.push(node->left);
            if (node->right) queue.push(node->right);
        }

        result.push_back(sum / size);
    }
    
    return result;
}

extern TreeNode* commonRoot;

int main() {
    std::vector<double> res = averageOfLevels(commonRoot);
    std::cout << "averageOfLevels: \n";
    printVector(res);
}